<template>
  <body id="poster">
    <el-form class="login-container" label-position="left" label-width="0px">
    <h3 class="login-title">系统登录</h3>
    <el-form-item >
      <el-input type="text" v-model="loginForm.username" placeholder="账号" auto-complete="off" size="normal"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input type="password" v-model="loginForm.password" placeholder="密码" auto-complete="off" size="normal"></el-input>
    </el-form-item>
    <el-form-item>
      <!-- `checked` 为 true 或 false -->
      <el-checkbox v-model="loginForm.checked">记住我</el-checkbox>
    </el-form-item>
    <el-form-item>
      <el-button class="login-button" type="primary" @click="login">登录</el-button>
      <el-button class="login-button" type="primary" @click="registerSkip">注册</el-button>
    </el-form-item>
  </el-form>
  </body>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      checked: true
    }
  },
  methods: {
    // 登录
    login () {
      var _this = this
      // 登录界面取得账户和密码到后端验证
      this.$axios
        .post('/login', {
          username: this.loginForm.username,
          password: this.loginForm.password,
        })
        .then(successResponse => {
          if (successResponse.data.code === 200) {
            // commit提交更改mutation中的login状态
            _this.$store.commit('login', _this.loginForm)
            // 获取url携带的redirect参数
            var path = this.$route.query.redirect
            // 导航到path，如果path为'/'或undefined则导航到/index
            this.$router.replace({ path: path === '/' || path === undefined ? '/index' : path })
          } else if (successResponse.data.code === 400) {
            // console.log('返回400，登录失败!')
            this.$alert(successResponse.data.message, '提示', {
              confirmButtonText: '确定'
            })
          }
        })
        .catch(failResponse => {
        })
    },
    // 注册页跳转
    registerSkip () {
      this.$router.replace('/register')
    }
  }
}
</script>

<style scoped>
  #poster{
    background: url("../assets/login-background.jpg") no-repeat;
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  .login-container{
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  .login-title{
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .login-button{
    background: skyblue;
    border: none;
  }
</style>
